//
//  MK_BarrageModel+ProtocolAdd.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/29.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_BarrageModel+ProtocolAdd.h"
#import "NSMutableAttributedString+Extension.h"

#define fontSize 10

@implementation MK_BarrageModel (ProtocolAdd)

-(NSMutableAttributedString *)barrageStr{
    NSMutableAttributedString* res = [NSMutableAttributedString new];
    if((self.rg.integerValue == MK_BarrageModel_UserIDentity_RoomManager1) ||self.rg.integerValue ==MK_BarrageModel_UserIDentity_RoomManager2){
        UIImage* image = [UIImage imageNamed:@"dy_fg"];
        [res addImageWithImage:image andFontSize:fontSize];
    }
    if (self.rg.integerValue == MK_BarrageModel_UserIDentity_SupperManager1||self.rg.integerValue == MK_BarrageModel_UserIDentity_SupperManager2) {
        UIImage* image = [UIImage imageNamed:@"dy_cg"];
        [res addImageWithImage:image andFontSize:fontSize];
    }
    //昵称
    [res appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:",self.nn] attributes:@{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}]];
    
    NSMutableAttributedString* mess = [[NSMutableAttributedString alloc] initWithString:self.txt attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSBaselineOffsetAttributeName:@(2)}];
    
    NSRegularExpression* expression = [NSRegularExpression regularExpressionWithPattern:@"\\[.*?\\]" options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray<NSTextCheckingResult*>* checkArr = [expression matchesInString:mess.string options:NSMatchingReportProgress range:NSMakeRange(0, mess.length)];
    for (NSTextCheckingResult* temRes in checkArr.reverseObjectEnumerator) {
        NSString* emoStr = [mess.string substringWithRange:temRes.range];
        NSString* emoName = [emoStr substringWithinBoundsLeft:@"[emot:" right:@"]"];
        
        UIImage* emoImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",emoName] inBundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"Emot.Bundle" ofType:nil]] compatibleWithTraitCollection:nil];
        
        NSTextAttachment* emoAtta = [[NSTextAttachment alloc]init];
        emoAtta.image = emoImage;
        emoAtta.bounds = CGRectMake(0, (fontSize-20)/2, 20, 20);
        NSAttributedString* emoAttStr = [NSAttributedString attributedStringWithAttachment:emoAtta];
        [mess replaceCharactersInRange:temRes.range withAttributedString:emoAttStr];
    }
    [res appendAttributedString:mess];
    return res;
}

@end


@implementation MK_UserLogInMessageModel (ProtocolAdd)

-(NSMutableAttributedString *)barrageStr{
    NSMutableAttributedString* res = [[NSMutableAttributedString alloc] init];
    if((self.rg.integerValue == MK_BarrageModel_UserIDentity_RoomManager1) ||self.rg.integerValue ==MK_BarrageModel_UserIDentity_RoomManager2){
        UIImage* image = [UIImage imageNamed:@"dy_fg"];
        [res addImageWithImage:image andFontSize:fontSize];
    }
    if (self.rg.integerValue == MK_BarrageModel_UserIDentity_SupperManager1||self.rg.integerValue == MK_BarrageModel_UserIDentity_SupperManager2) {
        UIImage* image = [UIImage imageNamed:@"dy_cg"];
        [res addImageWithImage:image andFontSize:fontSize];
    }
    [res appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"欢迎%@来到本直播间",self.nn] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName:[UIColor orangeColor]}]];

    
    return res;
}


@end
